/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-25 17:48:52
 * @LastEditors: TT
 * @LastEditTime: 2023-10-31 17:07:59
 */

import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/models/models_index.dart';

import 'package:hzy_project_template/utils/image_utils.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

class LoginApiUtilS {
  /// 登录
  static Future<HzyNormalResponse> loadLoginNetWork({
    required String account,
    required String pwd,
    bool isLogin = true,
  }) async {
    HzyNormalResponse response = HzyNormalResponse.fail(
      data: {},
      errorMsg: LaunchIdConfig.loginFail.tr,
    );
    var info = HzySpTool.getUserInfo(
      account: account,
    );
    UserModel userModel = UserModel();
    userModel.account = account;
    userModel.pwd = pwd;
    userModel.nickName = account;

    if (info == null) {
      Map info = userModel.toMap();
      response = HzyNormalResponse.success(
        netData: info,
      );
    } else {
      userModel = UserModel.fromMap(info);
      if (userModel.pwd == pwd || !isLogin) {
        response = HzyNormalResponse.success(
          netData: info,
        );
      }
    }
    return response;
  }

  static Future<HzyNormalResponse> loadRegisterNetWork({
    required String account,
    required String nickName,
    required String pwd,
    ImageNormalModel? imageNormalModel,
  }) async {
    var info = HzySpTool.getUserInfo(
      account: account,
    );
    UserModel userModel = UserModel();
    userModel.account = account;
    userModel.nickName = nickName;
    userModel.pwd = pwd;
    if (info == null) {
      userModel.headImage = imageNormalModel?.byte == null
          ? null
          : await ImageUtils.saveOnlyWithbytes(
              bytes: imageNormalModel!.byte!,
              type: "headerImage",
            );
      return HzyNormalResponse.success(
        netData: userModel.toMap(),
      );
    } else {
      return HzyNormalResponse.fail(
        data: {},
        errorMsg: LaunchIdConfig.register.tr +
            LaunchIdConfig.fail.tr +
            LaunchIdConfig.agree.tr +
            LaunchIdConfig.accountAlready.tr,
      );
    }
  }
}
